package org.eiahe.hr.attendance.util;

import lombok.experimental.UtilityClass;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

/**
 * 工龄计算工具类
 *
 * @author zhangnn
 */
@UtilityClass
public class WorkYearsUtil {

    /**
     * 计算工龄(年)
     * 
     * @param hireDate      入职日期
     * @param calculateDate 计算日期(通常是当前日期或年度核算日期)
     * @return 工龄(年),向下取整
     */
    public static int calculateWorkYears(LocalDate hireDate, LocalDate calculateDate) {
        if (hireDate == null || calculateDate == null) {
            return 0;
        }

        if (hireDate.isAfter(calculateDate)) {
            return 0;
        }

        Period period = Period.between(hireDate, calculateDate);
        return period.getYears();
    }

    /**
     * 计算工龄(年)，以当前日期为准
     * 
     * @param hireDate 入职日期
     * @return 工龄(年)
     */
    public static int calculateWorkYears(LocalDate hireDate) {
        return calculateWorkYears(hireDate, LocalDate.now());
    }

    /**
     * 计算工龄(天)
     * 
     * @param hireDate      入职日期
     * @param calculateDate 计算日期
     * @return 工龄(天)
     */
    public static long calculateWorkDays(LocalDate hireDate, LocalDate calculateDate) {
        if (hireDate == null || calculateDate == null) {
            return 0;
        }

        if (hireDate.isAfter(calculateDate)) {
            return 0;
        }

        return ChronoUnit.DAYS.between(hireDate, calculateDate);
    }

    /**
     * 计算工龄(月)
     * 
     * @param hireDate      入职日期
     * @param calculateDate 计算日期
     * @return 工龄(月)
     */
    public static long calculateWorkMonths(LocalDate hireDate, LocalDate calculateDate) {
        if (hireDate == null || calculateDate == null) {
            return 0;
        }

        if (hireDate.isAfter(calculateDate)) {
            return 0;
        }

        return ChronoUnit.MONTHS.between(hireDate, calculateDate);
    }
}
